/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Ronald
 */
public class TranslationServlet extends HttpServlet {
   
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet Translation</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet Translation at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */
        } finally { 
            out.close();
        }
    } 

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    public class Translation
    {
    String userInput;

    public Translation()
    {

    }
    private boolean beginsWithVowel (String userInput)
    {
        String vowels = "aeiouy";
        char letter = userInput.charAt(0);
        return (vowels.indexOf(letter) != -1);
    }
    private boolean beginsWithConsonant (String userInput)
    {
        String consonant = "bcdfghjklmnpqrstvwxz";
        char letter = userInput.charAt(0);
        return (consonant.indexOf(letter) != -0);
    }
    public String breakSentence(String sentence)
    {
        String output = "";
        userInput = sentence;
        sentence = sentence.toLowerCase();
        StringTokenizer st = new StringTokenizer(sentence);
        while (st.hasMoreTokens())
        {
            output += breakWord (st.nextToken());
            output += " ";
        }
        return output;
    }
    public String breakWord(String word)
    {
        String output = "";
        if(beginsWithVowel(word))
        {
            output = word + "way";
        } else
        {
            if(beginsWithConsonant(word))
            {
                output = word.substring(2) + word.substring(0,2) + "ay";
            } else
            {
                output = word.substring(1) + word.charAt(0) + "ay";
            }
        }
        return output;
    }
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
        PrintWriter pw = response.getWriter();
        pw.println("<html><head><title>PigLatinator - Pig Latin Translator</title></head><body><center><div style=\"border: 1px solid #000;padding:35px;width:700px;\"><img alt=\"PigLatinator\" src=\"logo.png\" /><br>");
        Translation translation = new Translation();
        pw.println("<br>");
        response.getWriter().println("<b>Your Input: </b> " + request.getParameter("userInput"));
        pw.println("<br><br>");
        response.getWriter().println("<b>Pig Latin:  </b>" + translation.breakSentence(request.getParameter("userInput")));
        pw.println("<br><br><a href=\"index.html\"><< translate another sentence >></a></center></div>");
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
